package com.beerandjesus.calc.bean;

import com.beerandjesus.calc.parse.TrackAndTime;
import com.beerandjesus.calc.parse.TracksAndTimes;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Takes input and prepares output of album track names and their
 * associated times.
 *
 * Adds track times together to supply expected stop points for splitting
 * full-album audio files into individual tracks.
 *
 * Parsing classes provide support for different formats of input.
 *
 * Date: Jan 27, 2008
 * Time: 4:12:51 PM
 */
public class CalculatorBean {
    List<String> trackNames = null;
    List<String> stops = null;
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("H'h' m:ss");
    private String both;
    private TracksAndTimes tracks = new TracksAndTimes();
    private boolean debug = false;

    public void setTrackNames(String tracks) {
        if (tracks == null) return;
        this.trackNames = new ArrayList<String>();

        String[] names = tracks.split("\n");
        for (String name : names) {
            trackNames.add(name.trim());
        }
    }

    public void setBoth(String both) {
        this.both = both;
        String[] lines = both.split("\n");
        for (String line : lines) {
            TrackAndTime trackAndTime = new TrackAndTime(line, TrackAndTime.InputFormat.ENDS_WITH_TIME);
            tracks.add(trackAndTime);
        }
    }

    public List<String> getTrackNames() {
        return trackNames;
    }

    public void setTrackTimes(String times) {
        if (times == null) return;
        stops = new ArrayList<String>();
        String[] nums = times.split("\n");
        calendar.clear();
        calendar.set(Calendar.HOUR, 0);
        for (int i = 0; i < nums.length; i++) {
            String num = nums[i];
            String[] minsAndSecs = num.split(":");
            String mins = minsAndSecs[0];
            String secs = minsAndSecs[1];
            if (mins == null || mins.equals("")) mins = "0";
            calendar.add(Calendar.MINUTE, Integer.parseInt(mins.trim()));
            calendar.add(Calendar.SECOND, Integer.parseInt(secs.trim()));
            stops.add(format.format(calendar.getTime()));
        }


    }

    public String getBoth() {
        return both;
    }

    public List<String> getStops() {
        return stops;
    }

    public String prepareOutput() {
        StringBuffer sb = new StringBuffer();
        sb.append("<table>\n");
        for (int i = 0; i < trackNames.size(); i++) {
            sb.append("<tr>\n<td>");
            sb.append(stops.get(i)).append("</td><td>").append(trackNames.get(i)).append("</td>\n</tr>\n");
        }
        sb.append("</table>\n");
        return sb.toString();
    }

    public String prepareOutputForBoth() {
        if (debug) {
            tracks.setDebug(true);
        }
        StringBuffer sb = new StringBuffer();
        sb.append("<table>\n").append("<tr>\n").append("<td>\n");
        sb.append(tracks.getTracks());
        sb.append("</td>\n<td>\n");
        sb.append(tracks.getTimes());
        sb.append("</td>\n</tr>\n</table>\n");
        return sb.toString();
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

}
